using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyWords
{
    public partial class MyWord : Form
    {
        public MyWord()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.Icon1;
        }

        protected override void WndProc(ref Message m)
        {
            //System.Diagnostics.Debug.WriteLine(m.ToString());

            if (m.Msg == 0x319)
            {
                if (m.LParam == new IntPtr(0x102e0000))
                {
                    // play
                    if (this.wordBrower1.Visible)
                    {
                        wordBrower1.Play();
                    }
                    else if (wordEditor1.Visible)
                    {
                        wordEditor1.Play();
                    }

                }
                else if (m.LParam == new IntPtr(0x102f0000))
                {
                    // pause
                    if (wordBrower1.Visible)
                    {
                        wordBrower1.Pause();
                    }
                    else if (wordEditor1.Visible)
                    {
                        wordEditor1.Pause();
                    }
                }
                else
                {
                    base.WndProc(ref m);
                }

            }
            else
            {

                base.WndProc(ref m);

            }

        }


        private void buttonExecQuery_Click(object sender, EventArgs e)
        {
            DataSet ds = DB.ExecDataSet(this.textBox1.Text);
            this.dataGrid1.DataSource = ds;
            this.dataGrid1.Refresh();
        }

        private void MyWord_Load(object sender, EventArgs e)
        {
            FrmRecordDevice aDlg = new FrmRecordDevice();
            aDlg.ShowDialog();
            MyAudio.AudioRecorder.m_Device = aDlg.CaptureDevice;
            MyAudio.AudioRecorder.m_Format = aDlg.SoundFormat;

            RestoreFormLocation();
            this.tabControl1.TabPages[0].Focus();
            this.wordBrower1.Active = this.wordBrower1.Visible;
            this.wordEditor1.Active = this.wordEditor1.Visible;
        }

        private void RestoreFormLocation()
        {
            try
            {
                System.Configuration.Configuration aConfiguration =
                    System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
                int x = Int32.Parse(aConfiguration.AppSettings.Settings["FormLocation_X"].Value);
                int y = Int32.Parse(aConfiguration.AppSettings.Settings["FormLocation_Y"].Value);
                int width = Int32.Parse(aConfiguration.AppSettings.Settings["FormWidth"].Value);
                int height = Int32.Parse(aConfiguration.AppSettings.Settings["FormHeight"].Value);

                this.Location = new Point(x, y);
                this.Width = width;
                this.Height = height;
            }
            catch
            {
            }
        }

        private void MyWord_FormClosing(object sender, FormClosingEventArgs e)
        {
            cswavrec.AudioPlayer.StopPlay();
            MyAudio.AudioRecorder.StopRecord();
            SaveFormLocation();
        }

        private void SaveFormLocation()
        {
            int x0 = 0;
            int y0 = 0;
            int width0 = 0;
            int height0 = 0;

            System.Configuration.Configuration aConfiguration =
                System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            try
            {
                x0 = Int32.Parse(aConfiguration.AppSettings.Settings["FormLocation_X"].Value);
                y0 = Int32.Parse(aConfiguration.AppSettings.Settings["FormLocation_Y"].Value);
                width0 = Int32.Parse(aConfiguration.AppSettings.Settings["FormWidth"].Value);
                height0 = Int32.Parse(aConfiguration.AppSettings.Settings["FormHeight"].Value);
            }
            catch
            {
            }

            try
            {
                int x = this.Location.X;
                int y = this.Location.Y;
                int width = this.Width;
                int height = this.Height;

                if (x == x0 && y == y0 && width == width0 && height == height0) return;
                if (width < 100 || height < 50) return;

                aConfiguration.AppSettings.Settings.Remove("FormLocation_X");
                aConfiguration.AppSettings.Settings.Remove("FormLocation_Y");
                aConfiguration.AppSettings.Settings.Remove("FormWidth");
                aConfiguration.AppSettings.Settings.Remove("FormHeight");

                aConfiguration.AppSettings.Settings.Add("FormLocation_X", x.ToString());
                aConfiguration.AppSettings.Settings.Add("FormLocation_Y", y.ToString());
                aConfiguration.AppSettings.Settings.Add("FormWidth", width.ToString());
                aConfiguration.AppSettings.Settings.Add("FormHeight", height.ToString());

                aConfiguration.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            wordEditor1.Active = false;
            wordBrower1.Active = false;

            if (wordBrower1.Visible)
            {
                wordBrower1.Active = true;
            }
            else if (wordEditor1.Visible)
            {
                wordEditor1.Active = true;
            }

        }

    }
}